package cn.cloudheritageservice8002.service.impl;

import cn.cloudheritageservice8002.entity.CulturalHeritage;
import cn.cloudheritageservice8002.mapper.CulturalHeritageMapper;
import cn.cloudheritageservice8002.service.CulturalHeritageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collections;

import java.util.List;

@Service
@Transactional
public class CulturalHeritageServiceImpl implements CulturalHeritageService {
    
    private static final Logger logger = LoggerFactory.getLogger(CulturalHeritageServiceImpl.class);
    
    @Autowired
    private CulturalHeritageMapper mapper;

    @Override
    public List<CulturalHeritage> search(String keyword) {
        logger.info("执行搜索查询，关键词: {}", keyword);
        if (keyword == null || keyword.trim().isEmpty()) {
            logger.warn("搜索关键词为空，返回空列表");
            return Collections.emptyList();
        }
        
        try {
            List<CulturalHeritage> result = mapper.searchHeritage(keyword.trim());
            logger.info("搜索到 {} 条记录", result != null ? result.size() : 0);
            if (result != null && !result.isEmpty()) {
                logger.debug("搜索结果示例: {}", result.get(0));
            }
            return result != null ? result : Collections.emptyList();
        } catch (Exception e) {
            logger.error("搜索文化遗产时发生异常，关键词: " + keyword, e);
            throw e;
        }
    }

    @Override
    public CulturalHeritage createHeritage(CulturalHeritage heritage) {
        logger.info("创建文化遗产: {}", heritage);
        int result = mapper.insert(heritage);
        logger.info("插入记录结果: {}, 生成ID: {}", result > 0 ? "成功" : "失败", heritage.getId());
        return heritage;
    }

    @Override
    public CulturalHeritage updateHeritage(CulturalHeritage heritage) {
        logger.info("更新文化遗产，ID: {}, 数据: {}", heritage.getId(), heritage);
        int result = mapper.updateByPrimaryKeySelective(heritage);
        logger.info("更新记录结果: {}, 影响行数: {}", result > 0 ? "成功" : "失败", result);
        return heritage;
    }

    @Override
    public boolean deleteHeritage(Long id) {
        logger.info("删除文化遗产，ID: {}", id);
        int result = mapper.deleteByPrimaryKey(id);
        boolean success = result > 0;
        logger.info("删除记录结果: {}, 影响行数: {}", success ? "成功" : "失败", result);
        return success;
    }

    @Override
    public List<CulturalHeritage> listHeritage() {
        logger.info("查询所有文化遗产");
        List<CulturalHeritage> result = mapper.selectAll();
        logger.debug("查询到 {} 条记录", result != null ? result.size() : 0);
        return result;
    }
    
    @Override
    public CulturalHeritage getHeritageById(Long id) {
        try {
            logger.info("根据ID查询文化遗产，ID: {}", id);
            if (id == null) {
                logger.error("ID不能为null");
                throw new IllegalArgumentException("ID不能为null");
            }
            logger.debug("调用mapper.selectByPrimaryKey({})", id);
            CulturalHeritage heritage = mapper.selectByPrimaryKey(id);
            logger.debug("查询结果: {}", heritage != null ? "找到记录" : "未找到记录");
            if (heritage != null) {
                logger.debug("查询到的数据: {}", heritage);
            }
            return heritage;
        } catch (Exception e) {
            logger.error("根据ID查询文化遗产时发生异常，ID: " + id, e);
            throw e;
        }
    }
}
